<template>
  <div class="user-info" v-loading.fullscreen="loading">
    <el-card class="basicInfo-pannel">
      <div class="input-avatar">
        <img class="avatar" :src="avatarUrl" />
        <input
          class="input-file"
          type="file"
          @change="uploadAvatar"
          ref="avatar_upload"
        />
      </div>
      <p class="username" v-if="!editUsername">用户名:{{ userName }}</p>
      <el-input
        type="text"
        class="update-username"
        v-model="userName"
        v-if="editUsername"
        @blur="updateUserInfo"
        @keyup.enter.native="enterBlur($event)"
      ></el-input>
      <div class="edit_icon1" @click="editUsername = !editUsername">
        <i class="el-icon-edit-outline"></i>
      </div>
      <p class="userType">{{ userType }}</p>
    </el-card>
    <el-card class="addInfo-pannel">
      <p class="addInfo-label">基本信息</p>
      <el-divider class="divider"></el-divider>
      <div class="message-pannel">
        <div class="pannel-left">
          <label class="field">用户ID</label>
          <label class="field">发表文章数</label>
          <label class="field">邮箱</label>
        </div>
        <div class="pannel-right">
          <p class="field-value">{{ userId }}</p>
          <p class="field-value">{{ articleInfo.total }}</p>
          <p class="field-value" v-if="!editEmail">{{ userEmail }}</p>
          <el-input
            type="text"
            class="update-field-value"
            v-if="editEmail"
            v-model="userEmail"
            @blur="updateUserInfo"
            @keydown.enter.native="enterBlur($event)"
          ></el-input>
        </div>
        <div class="edit_icon2" @click="editEmail = !editEmail">
          <i class="el-icon-edit-outline"></i>
        </div>
      </div>
    </el-card>
    <el-card class="operation-panel">
      <div class="modify-password" @click="showDialog = true">
        <p>修改密码</p>
      </div>
      <el-dialog
        title="修改密码"
        :visible.sync="showDialog"
        width="50%"
        top="15vh"
      >
        <el-form>
          <el-form-item label="当前密码">
            <el-input type="password" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item label="新密码">
            <el-input type="password" v-model="newPassword"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码">
            <el-input type="password" v-model="confirmNewPassword"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="modifyPassword">确 定</el-button>
        </span>
      </el-dialog>
      <el-divider class="divider"></el-divider>
      <div class="article-manage" @click="toArticleManager">
        <p>内容管理</p>
      </div>
    </el-card>
  </div>
</template>

<script>
import { api } from "@/api";
import { StoreHandler } from "@/store";
export default {
  data() {
    return {
      editUsername: false,
      editEmail: false,
      articleInfo: {
        list: [],
        total: 0,
      },
      loading: false,
      showDialog: false,
      user: {
        id: 0,
        username: "",
        password: "",
      },
      newPassword: "",
      confirmNewPassword: "",
    };
  },
  methods: {
    //上传头像
    async uploadAvatar() {
      let file = this.$refs.avatar_upload.files[0];
      await this.$axios(api.user.uploadAvatar(file));
      StoreHandler.setters.setAvatarUrl(
        "/api/" +
          api.user.getAvatar(this.userInfo.username).url +
          `?${Math.random()}`
      );
    },
    async loadArticleInfo(aid = null) {
      try {
        this.loading = true;
        this.articleInfo = (
          await this.$axios(api.article.getArticleList(aid))
        ).data;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        if (error.response) {
          this.$alert(error.response.data.message);
        } else {
          this.$alert("网络错误");
        }
      }
    },
    async updateUserInfo() {
      let user = {
        username: this.userName,
        email: this.userEmail,
      };
      try {
        await this.$axios(api.user.modifyUserInfo(user));
        this.editUsername = false;
        this.editEmail = false;
        this.loadArticleInfo(this.userId);
      } catch (error) {
        if (error.response) {
          this.$alert(error.response.data.message);
        } else {
          this.$alert("网络错误");
        }
      }
    },
    enterBlur(event) {
      event.target.blur();
    },
    async modifyPassword() {
      try {
        if (this.user.password === "" || this.newPassword === "") {
          this.$alert("密码不能为空");
          return;
        }
        if (this.user.password === this.newPassword) {
          this.$alert("新密码不能是当前密码");
          return;
        }
        if (this.newPassword !== this.confirmNewPassword) {
          this.$alert("两次新密码输入不一致");
          return;
        }
        this.user.id = this.userId;
        this.user.username = this.userName;
        await this.$axios(api.user.login(this.userName, this.user.password));
        this.user.password = this.newPassword;
        await this.$axios(api.user.modifyUserPassword(this.user));
        this.showDialog = false;
        this.$alert("修改成功!");
      } catch (error) {
        this.showDialog = false;
        if (error.response.data.code == 401) {
          this.$alert("密码错误!");
          this.user.password = "";
          this.Password = "";
          return;
        } else {
          this.$alert(error.response.data.message);
        }
      }
    },
    toArticleManager() {
      this.$router.push({ path: "/manager", query: { aid: this.userId } });
    },
  },
  computed: {
    userInfo() {
      return StoreHandler.getters.getUserInfo();
    },
    avatarUrl() {
      return StoreHandler.getters.getAvatarUrl();
    },
    userType() {
      return this.userInfo
        ? this.userInfo.admin
          ? "管理员"
          : "普通用户"
        : "未登录";
    },
    userName: {
      get() {
        return this.userInfo ? this.userInfo.username : "未登录";
      },
      set(value) {
        this.$store.state.userInfo.username = value;
      },
    },
    userId() {
      return this.userInfo ? this.userInfo.id : null;
    },
    userEmail: {
      get() {
        return this.userInfo ? this.userInfo.email : "未设置邮箱";
      },
      set(value) {
        this.$store.state.userInfo.email = value;
      },
    },
  },
  async mounted() {
    await this.$parent.loadUserInfo();
    await this.loadArticleInfo(this.$store.state.userInfo.id);
  },
};
</script>

<style lang="less" scoped>
.user-info {
  margin-top: 60px;
  .basicInfo-pannel {
    margin: 0 auto;
    width: 60%;
    height: 150px;
    min-width: 200px;
    .userType {
      color: darkgray;
      position: absolute;
      margin: 60px 200px;
    }
    .username {
      position: absolute;
      margin: 10px 200px;
      font-size: 20px;
      font-weight: bold;
    }
    .update-username {
      position: absolute;
      width: 180px;
      margin: 10px 200px;
    }
    .edit_icon1 {
      position: absolute;
      margin: 17px 400px;
      cursor: pointer;
    }
    .input-avatar {
      position: absolute;
      width: 56%;
      height: 180px;
      min-width: 200px;
      .avatar {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        margin: 0 50px;
        position: absolute;
      }
      .input-file {
        cursor: pointer;
        position: absolute;
        opacity: 0;
        border-radius: 50%;
        margin: 0 50px;
        width: 100px;
        height: 100px;
      }
    }
  }
  .addInfo-pannel {
    transform: translate(0, 20px);
    margin: 0 auto;
    width: 60%;
    height: 300px;
    min-width: 200px;
    .message-pannel {
      display: flex;
      .pannel-left {
        flex: 1;
        .field {
          margin: 40px 40px;
          display: block;
        }
      }
      .pannel-right {
        flex: 1;
        flex-direction: row;
        .field-value {
          font-weight: bold;
          margin: 42px 0;
        }
        .update-field-value {
          width: 200px;
          height: 20px;
        }
      }
      .edit_icon2 {
        display: inline;
        margin-top: 165px;
        cursor: pointer;
      }
    }
    .divider {
      margin: 5px 0;
    }
    .addInfo-label {
      font-weight: bold;
      font-size: 16px;
      margin: 0;
    }
  }
  .operation-panel {
    margin: 0 auto;
    width: 60%;
    min-width: 200px;
    height: 150px;
    margin-top: 40px;
    .modify-password {
      cursor: pointer;
    }
    .article-manage {
      margin-top: 10px;
      cursor: pointer;
    }
  }
}
</style>
